#include "employeeservice.h"

bool EmployeeService::registerEmployee(const QString& employeeId, const QString& name,
                                       const QString& department, const QString& position,
                                       const QByteArray& photoData) {
    if (employeeId.isEmpty() || name.isEmpty())
    {
        qWarning() << "Employee ID and name cannot be empty";
        return false;
    }

    // 准备带参数的 SQL（所有字段都用参数绑定，避免 SQL 注入）
    QString sql = R"(
                  INSERT INTO employee (employee_id, name, department, position, photo)
                  VALUES (:employee_id, :name, :department, :position, :photo)
                  )";

    // 调用数据库管理器执行带参数的查询
    return dbManager->execNonQueryWithParams(sql, [&](QSqlQuery &query)
    {
        // 绑定所有参数（包括二进制照片数据）
        query.bindValue(":employee_id", employeeId);
        query.bindValue(":name", name);
        query.bindValue(":department", department);
        query.bindValue(":position", position);
        query.bindValue(":photo", photoData); // 绑定二进制数据
        return true; });
}

QJsonArray EmployeeService::getAllEmployee() {
    QJsonArray array;
    QSqlQuery query = dbManager->execQuery("SELECT * FROM employee");

    while (query.next()) {
        QJsonObject obj;
        obj["id"] = query.value("id").toInt();
        obj["employeeId"] = query.value("employee_id").toString();
        obj["name"] = query.value("name").toString();
        obj["department"] = query.value("department").toString();
        obj["position"] = query.value("position").toString();
        // 注意：照片数据通常不直接返回，如需返回可加字段
        array.append(obj);
    }
    return array;
}

QByteArray EmployeeService::getEmployeePhoneById(const QString& employeeId){
    if (employeeId.isEmpty()) {
        qWarning() << "Employee ID cannot be empty";
        return QByteArray();
    }

    // 带参数查询，避免SQL注入
    QString sql = "SELECT photo FROM employee WHERE employee_id = :employee_id";
    QSqlQuery query = dbManager->execQueryWithParams(sql, [&](QSqlQuery &q) {
            q.bindValue(":employee_id", employeeId);
            return true;
});

    if (query.next()) {
        // 返回照片二进制数据
        return query.value("photo").toByteArray();
    }

    // 未找到对应员工时返回空字节数组
    return QByteArray();
}

QList<QPair<QString, QByteArray>> EmployeeService::getAllEmployeePhotos()
{
    QList<QPair<QString, QByteArray>> result;
    // 查询所有员工的ID和照片
    QSqlQuery query = dbManager->execQuery("SELECT employee_id, photo FROM employee");

    while (query.next())
    {
        QString employeeId = query.value("employee_id").toString();
        QByteArray photoData = query.value("photo").toByteArray();
        if (!employeeId.isEmpty() && !photoData.isEmpty())
        {
            result.append(QPair<QString, QByteArray>(employeeId, photoData));
        }
    }
    return result;
}

QJsonArray EmployeeService::getEmployeesByName(const QString& name) {
    QJsonArray array;
    if (name.isEmpty()) {
        qWarning() << "Employee name cannot be empty";
        return array;
    }

    // 构造模糊查询SQL（参数化查询防止注入）
    QString sql = "SELECT * FROM employee WHERE name LIKE :name";

    QSqlQuery query = dbManager->execQueryWithParams(sql, [&](QSqlQuery& q) {
            // 前后加%实现模糊匹配（匹配包含name的所有记录）
            q.bindValue(":name", "%" + name + "%");
            return true;
});

    while (query.next()) {
        QJsonObject obj;
        obj["id"] = query.value("id").toInt();
        obj["employeeId"] = query.value("employee_id").toString();
        obj["name"] = query.value("name").toString();
        obj["department"] = query.value("department").toString();
        obj["position"] = query.value("position").toString();
        QByteArray photoData = query.value("photo").toByteArray();
        obj["photo"] = QString(photoData.toBase64());
        array.append(obj);
    }
    return array;
}
